package me.zhengjie.modules.management.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.management.domain.*;
import me.zhengjie.modules.management.mapper.ConferenceRoomMapper;
import me.zhengjie.modules.management.mapper.MrConferencePurposeMapper;
import me.zhengjie.modules.management.mapper.RoomPicturesMapper;
import me.zhengjie.modules.management.service.ConferenceRoomService;
import me.zhengjie.modules.management.service.dto.ConferenceRoomQueryCriteria;
import me.zhengjie.utils.PageHelpResultUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: yuchao.wang
 * @Date: 2020/12/7
 * @Description :
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class ConferenceRoomServiceImpl implements ConferenceRoomService {
    private final ConferenceRoomMapper conferenceRoomMapper;

    private final MrConferencePurposeMapper  PurposeMapper;

    private final RoomPicturesMapper roomPicturesMapper;

    @Override
    public Map<String, Object> findByCriteria(ConferenceRoomQueryCriteria conferenceRoomQueryCriteria, Pageable pageable) {
        PageHelper.startPage(pageable.getPage(), pageable.getSize());
        List<ConferenceRoom> conferenceRoomList = conferenceRoomMapper.queryByCriteria(conferenceRoomQueryCriteria);
        for (ConferenceRoom conferenceRoom : conferenceRoomList) {
            conferenceRoom.setUrls(roomPicturesMapper.findUrlByRoomId(conferenceRoom.getRoomId()));
        }
        PageInfo<ConferenceRoom> pageInfo = new PageInfo(conferenceRoomList);
        return PageHelpResultUtil.toPage(pageInfo);
    }

    //根据教室id查用途
    @Override
    public List<MrConferencePurpose> findPurposeByRoomId(Integer roomId) {
        List<MrConferencePurpose> list=new ArrayList();
        String purposes = conferenceRoomMapper.findRoomPurposeById(roomId);
        String[] purpose = purposes.split("\\,");
        for (String s : purpose) {
            list.add(PurposeMapper.getMrConferencePurposeById(Integer.parseInt(s)));
        }
        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(ConferenceRoom resources) {

        // 字段非空校验
        nullTypeTest(resources);
        // 插入职位信息
        try {
            conferenceRoomMapper.insertConferenceRoom(resources);
            RoomPictures roomPictures = new RoomPictures();
            //插入教室图片
            if(resources.getUrls().size()>0){
                for (String url : resources.getUrls()) {
                    roomPictures.setRoomId(resources.getRoomId());
                    roomPictures.setUrl(url);
                    roomPicturesMapper.addPictures(roomPictures);
                }
            }
        } catch (Exception e) {
            throw new BadRequestException("新增操作失败！");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ConferenceRoom resources) {

        // 字段非空校验
        nullTypeTest(resources);
        // 编辑职位
        try {
            conferenceRoomMapper.updateConferenceRoom(resources);
            //根据roomId删除图片表里面所有的图片，再进行图片的新增
            if(resources.getUrls().size()>0){
                roomPicturesMapper.deleteByRoomId(resources.getRoomId());
                RoomPictures roomPictures = new RoomPictures();
                for (String url : resources.getUrls()) {
                    roomPictures.setRoomId(resources.getRoomId());
                    roomPictures.setUrl(url);
                    roomPicturesMapper.addPictures(roomPictures);
                }
            }

        } catch (RuntimeException e) {
            throw new BadRequestException("更新操作失败！");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Integer> ids) {
        // 删除
        try {
            for (Integer id : ids) {
                conferenceRoomMapper.deleteConferenceRoom(id);
            }
        } catch (RuntimeException e) {
            throw new BadRequestException("删除操作失败！");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<ConferenceRoom> findById(Integer roomId){
        return conferenceRoomMapper.findById(roomId);
    }




    /**
     * 对字段进行非空校验（FOrgID,FDeptID,FPostCode,FPostName）
     *
     * @param resources
     * @return void
     */
    public void nullTypeTest(ConferenceRoom resources) {

        if (ObjectUtil.isNull(resources.getRoomName())) {
            throw new BadRequestException("教室名称不能为空！");
        }

        if (ObjectUtil.isNull(resources.getTypeId())) {
            throw new BadRequestException("教室类型不能为空！");
        }

        if (ObjectUtil.isNull(resources.getPurposeId())) {
            throw new BadRequestException("教室用途不能为空！");
        }

        if (ObjectUtil.isNull(resources.getEquipId())) {
            throw new BadRequestException("设备类型不能为空！");
        }

        if (ObjectUtil.isNull(resources.getCost())) {
            throw new BadRequestException("能源费用不能为空！");
        }
    }
}
